Skip to content

encoding/json/v2: reduce allocations when checking IsZero - #80879

Open
philpearl wants to merge 1 commit into
golang:masterfrom
philpearl:omitzero
Open

encoding/json/v2: reduce allocations when checking IsZero#80879
philpearl wants to merge 1 commit into
golang:masterfrom
philpearl:omitzero

Conversation

@philpearl

@philpearl philpearl commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

When checking whether an omitzero field is zero via an IsZero method,
calling Interface() on a value type causes a heap allocation due to
interface boxing.

Reorder the switch cases to check if reflect.PointerTo(sf.Type)
implements the isZeroer interface before checking sf.Type directly.
Because pointer method sets include value methods, this allows using
va.Addr().Interface(), which avoids boxing value types and eliminates
the extra allocation per field.

Fixes #80868

… omitzero field implements IsZero

Where I work we have structs with many small fields with IsZero methods. Marshalling these structs with json/v2 causes many allocations, one per such field. I tracked the allocation down to the code that checks for and calls the IsZero method. Calling reflect.Value.Interface{} causes an allocation when the value is not a pointer type.

Removing the excess allocation is as easy as switching the order of two lines in a switch statement so that the case that tries to use a pointer to the field is tried first.

Before:

BenchmarkMarshal/OmitZeroMethod-10  	14441822	        83.81 ns/op	  23.86 MB/s	      16 B/op	       2 allocs/op

After

BenchmarkMarshal/OmitZeroMethod-10  	14114629	        80.38 ns/op	  24.88 MB/s	       8 B/op	       1 allocs/op

Fixes golang#80868

Change-Id: I3c1da4946e16a94e70658cdfa02e2b5e29b5c6a5
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: e3c0c26) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/815400.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Phil Pearl:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from t hepudds:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Phil Pearl:

Patch Set 2:

(3 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from qiu laidongfeng:

Patch Set 3: Commit-Queue+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 3:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2026-08-14T14:50:59Z","revision":"b965966123b9079bf09cf89ba8d276b624ecea46"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from qiu laidongfeng:

Patch Set 3: -Commit-Queue

(Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_64033>)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 3:

This CL has passed the run


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 3: LUCI-TryBot-Result+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Sean Liao:

Patch Set 3:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Phil Pearl:

Patch Set 3:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Sean Liao:

Patch Set 3:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@philpearl

Copy link
Copy Markdown
Contributor Author

Removed this from the PR description in an attempt to fix some Gerrit stuff that I don't understand

Change-Id: I3c1da4946e16a94e70658cdfa02e2b5e29b5c6a5

@philpearl

Copy link
Copy Markdown
Contributor Author

Does kicking this again do anything?

@philpearl philpearl changed the title encoding/json/v2: reduce marshal allocs when an omitzero field has IsZero encoding/json/v2: reduce allocations when checking IsZero Aug 14, 2026
@philpearl

Copy link
Copy Markdown
Contributor Author

Come on gerrit bot. Do something!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Phil Pearl:

Patch Set 3:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Phil Pearl:

Patch Set 5:

(3 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from t hepudds:

Patch Set 5:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from t hepudds:

Patch Set 6:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815400.
After addressing review feedback, remember to publish your drafts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

encoding/json/v2: Unnecessary allocation marshalling struct with IsZero method and omitzero

2 participants